package com.google.wave.poppy.mail;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SendMail
{
	private static final Logger LOG = Logger.getLogger(SendMail.class.getName());
	private static String urlEncode(String x) {
		return URLEncoder.encode(x);
	}

	public static boolean sendEmail(String waveID, String recipient, String subject, String body) {
		//String urlString = "http://syncstr.com/send.php?";
		String urlString = "http://syncstr.com/sendwave.php";
		String message = "id=" + urlEncode(waveID) +
						 "&to=" + urlEncode(recipient) +
						 "&subject=" + urlEncode(subject) +
						 "&body=" + urlEncode(body);
		// Post Style
		try {
			//urlString = urlString + message;
			URL url = new URL(urlString);
			
			// Post Style.
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setRequestProperty("Content-Length", Integer.toString(message.getBytes().length));
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(message);
            writer.close();
            
            // We need to call something on the response to make the app engine make the real request.
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // OK
            } else {
                // Server returned HTTP error code.
            }

            LOG.log(Level.SEVERE, "Post Request Successful");
			
			// Get Style.
			/*BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String line;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			reader.close();*/
		} catch (MalformedURLException e) {
			LOG.log(Level.SEVERE, "MalformedURLException" + e.getMessage());
			return false;
		} catch (IOException e) {
			LOG.log(Level.SEVERE, "IOException" + e.getMessage());
			return false;
		}
		return true;
	}

//	public static boolean sendEmailOLD(MimeMessage message) {
//		try {
//			Transport.send(message);
//		} catch (AddressException e) {
//			e.printStackTrace();
//			return false;
//		} catch (MessagingException e) {
//			e.printStackTrace();
//			return false;
//		}
//		return true;
//	}

//	public static MimeMessage newMimeMessageToSubjBody(String senderName, String recipient, String subject, String body) {
//		Properties props = new Properties();
//		Session session = Session.getDefaultInstance(props, null);
//		MimeMessage msg = new MimeMessage(session);
//		try {
//			msg.setFrom(new InternetAddress("elliston@gmail.com", senderName));
////			msg.setFrom(new InternetAddress("kewaljit.nagpal@gmail.com", senderName));
//			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient, recipient));	// TODO: Second param is user's name
//			msg.setSubject(subject);
//			msg.setText(body);
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (MessagingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return msg;
//	}
}
